
var ip = function(IP){
	let IPv4 = /^((2([0-4]\d | 5[0-5]) | 1\d\d | [1-9]\d | \d)\.){3}(2([0-4]\d | 5[0-5]) | 1\d\d | [1-9]\d | \d)$/
    let IPv6 = /^([\da-fA-F]{4}\:){7}[\da-fA-F]$/
    return IPv4.test() ? "IPv4" : IPv6.test() ? "IPv6" : "neither"
}
/**
 * @param {string} queryIP
 * @return {string}
 */
 var validIPAddress = function(ip) {
	//判断是否有"."
    if(ip.indexOf(".") != -1){
        //将字符串以"."分隔
        let arr = ip.split(".")
        //判断数组长度
        if(arr.length != 4){
            return "Neither"
        }
        //遍历数组，取出每个参数进行IPv4的特征比较
        for(i = 0;i < 4;i++){
            let n = arr[i].length
            if(arr[i] >= 0 && arr[i] <= 255 && n > 0 && n < 4){
                //只有一位时可以为0,以上时第一位不能为0
                if(n == 1 && n >= 0 && n <= 9){
                    flag = true
                }else if(Number(arr[i]).toString() === arr[i] && arr[i] >= 0 && arr[i] <= 255 && arr[i][0] != 0){
                    flag = true
                }else{
                    flag = false
                    break
                }
            }else{
                flag = false
                break
            }
        }
        if(flag){
            return "IPv4"
        }else{
            return "Neither"
        }
    }

    if(ip.indexOf(":") != -1){
        let arr = ip.split(":")
        if(arr.length != 8){
            return "Neither"
        }
        for(i = 0;i < 8;i++){
            let n = arr[i].length
            if(n <= 4 && n >= 1 && ("0x" + arr[i]) >= 0 && ("0x" + arr[i]) <= 0xFFFF){
                flag = true
            }else{
                flag = false
                break
            }
        }
        if(flag){
            return "IPv6"
        }else{
            return "Neither"
        }
    }
    return "Neither"
};
var a = Date.now()
console.log(validIPAddress('01.1.1.1'))
var b = Date.now()
console.log(b - a)
var a = Date.now()
console.log(ip('01.1.1.1'))
var b = Date.now()
console.log(b - a)